// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    totalPrice:0,
    totalNum:0,
    selectAll:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    const address = wx.getStorageSync('address');
    const cart = wx.getStorageSync('cart')
    this.isAllSelected();
    this.handleTotal(cart);
    this.setData({
      address,
      cart,
    })
  },
  handleGoodsCount(e) {
    this.data.cart.forEach(item => {
      if (item.goods_id === e.currentTarget.dataset.item.goods_id) {
        return item.num++
      }
    })
    

  },
  handleChooseAddress() {
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        wx.setStorageSync("address", result);
      },
      fail: () => {},
      complete: () => {}
    });
  },
  // 商品选中
  selectGoods(e) {
    let {
      cart
    } = this.data;
    let index = this.data.cart.findIndex(v => v.goods_id === e.target.dataset.id);
    console.log(index)
    cart[index].checked = !cart[index].checked;
    this.setCart(cart)

  },
  setCart(cart) {
    let totalPrice = 0;
    let totalNum = 0;
    let allChecked = true;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.goods_price * v.num;
        totalNum += v.num;
      } else {
        allChecked = false
      }
    });
  },
  handleSelectGood(e){
    this.data.cart.forEach((item,i)=>{
      const ele = this.data.cart[i];
      const cat_id = e.target.dataset.item.cat_id;
      if(cat_id == ele.cat_id){
        ele.checked = !!e.detail.value[0];
      }
    })
    wx.setStorageSync('cart',this.data.cart)
    this.isAllSelected()
  },
  isAllSelected(){
    const selectAll = this.data.cart.every((item,i)=>{
      return item.checked
    })
    this.setData({selectAll})
    this.handleTotal(this.data.cart);
  },
  switchAllSelct(){
    let selectAll = this.data.selectAll;
    selectAll = !selectAll;
    let cart = this.data.cart;
    this.data.cart.forEach((item,i)=>{
      let ele = this.data.cart[i];
      ele.checked = selectAll;
    })
    this.setData({
      cart,
      selectAll
    })
    this.handleTotal(cart);
  },
  handleItemCount(e){
    console.log('e',e,e.currentTarget.dataset.type)
    const type = e.currentTarget.dataset.type;
    let cat_id = e.target.dataset.id;
    this.data.cart.map((item,i)=>{
      if(item.cat_id == cat_id){
        if(type ==='minus'){
          this.data.cart[i].num>1?this.data.cart[i].num--:this.data.cart[i].num;
        }else{
          this.data.cart[i].num++;
        }
      }
    })
    this.handleTotal( this.data.cart)
    wx.setStorageSync('cart',this.data.cart)
    this.setData({
      cart:this.data.cart
    })
  },
  handleTotal(cart){
    let totalNum = 0;
    let totalPrice = 0;
    cart.reduce((total,item)=>{
      if(item.checked) {
        totalPrice  += item.goods_price * item.num
        totalNum += item.num;
      } 
    },0)
    this.setData({
      totalNum,
      totalPrice
    })
  
  }
})